const {EnumBehavior} = require("./Enum");
const {findLink} = require("./Find");
module.exports.towerBehavior=(tower)=>{
    switch (tower.room.memory.towerBehavior){
        case EnumBehavior.RepairBuild:
            let repairBuild=Game.getObjectById(tower.room.memory.towerRelateId);
            tower.repair(repairBuild);
            if(repairBuild===null||repairBuild.structureType===null){
                tower.room.memory.towerBehavior=EnumBehavior.Free;
                tower.room.memory.towerRelateId=undefined;
            }
            let finish=false;
            if(repairBuild.structureType===STRUCTURE_WALL){
                if(repairBuild.hits > 50000 * tower.room.controller.level){
                    tower.room.memory.towerBehavior=EnumBehavior.Free;
                    tower.room.memory.towerRelateId=undefined;
                }
            }else if (repairBuild.structureType===STRUCTURE_RAMPART){
                if(repairBuild.hits > 30000 * tower.room.controller.level){
                    tower.room.memory.towerBehavior=EnumBehavior.Free;
                    tower.room.memory.towerRelateId=undefined;
                }
            }else{
                if(repairBuild.hits===repairBuild.hitsMax){
                    tower.room.memory.towerBehavior=EnumBehavior.Free;
                    tower.room.memory.towerRelateId=undefined;
                }
            }
            break;
        case EnumBehavior.HealCreep:
            let healCreep=Game.getObjectById(tower.room.memory.towerRelateId);
            tower.heal(healCreep);
            if(healCreep===null||healCreep.hits===healCreep.hitsMax){
                tower.room.memory.towerBehavior=EnumBehavior.Free;
                tower.room.memory.towerRelateId=undefined;
            }
            break;
        case EnumBehavior.AttackCreep:
            let invasionCreep=Game.getObjectById(tower.room.memory.towerRelateId);
            let state=tower.attack(invasionCreep)
            if(invasionCreep===undefined||state===-7){
                tower.room.memory.towerBehavior=EnumBehavior.Free;
                tower.room.memory.towerRelateId=undefined;
            }
            break;
    }
}

module.exports.distributeTower=(room, roomCreepList, repairBuildList)=>{
    //build behavior
    let invasionCreepList = room.find(FIND_HOSTILE_CREEPS);
    if (invasionCreepList.length > 0) {
        let healInvasionList = invasionCreepList.filter(creep => {
            return creep.body.some(part => part.type === HEAL)
        }).sort((a, b) => {
            const aHeals = a.body.filter(part => part.type === HEAL).length;
            const bHeals = b.body.filter(part => part.type === HEAL).length;
            return bHeals - aHeals;
        });
        //治疗能量较强的兵种
        if (healInvasionList.length > 0) {
            room.memory.towerRelateId = healInvasionList[0].id;
        } else {
            room.memory.towerRelateId = invasionCreepList[0].id;
        }
        room.memory.towerBehavior = EnumBehavior.AttackCreep;
        return;
    }
    if (repairBuildList.length > 0) {//RepairBuild
        room.memory.towerBehavior = EnumBehavior.RepairBuild;
        room.memory.towerRelateId = repairBuildList[0].id;
        return;
    }
    let injuredCreepList = roomCreepList.filter(creep => creep.hits < creep.hitsMax);
    if (injuredCreepList.length > 0) {//HealCreep
        room.memory.towerBehavior = EnumBehavior.HealCreep;
        room.memory.towerRelateId = injuredCreepList[0].id;
    }
}

module.exports.distributeLink=(room, butlerList)=>{
    let baseLinkId = room.memory.baseLink;
    let upLinkId = room.memory.upLink;
    let baseLink = null;
    let upLink = null;
    //baseLink behavior
    if (baseLinkId) {
        //bind creep
        baseLink = Game.getObjectById(baseLinkId);
        if (baseLink && baseLink.usedStore() > 500) {
            let closeButler = baseLink.pos.findClosestByRange(butlerList);
            if (closeButler !== null) {
                closeButler.bindBehavior(EnumBehavior.TransLink, baseLinkId);
            }
        }
    }
    if (upLinkId) {
        upLink = Game.getObjectById(upLinkId);
    }
    let linkList = findLink(room).filter(build => {
        return build.id !== room.memory.baseLink && build.id !== room.memory.upLink
    })
    for (let link of linkList) {
        if (link.usedStore() > 400) {
            if (upLink !== null && upLink.freeStore() > 100) {
                link.transferEnergy(upLink);
                break;
            }
            if (baseLink !== null && baseLink.freeStore() > 100) {
                link.transferEnergy(baseLink);
                break;
            }
        }
    }
}

module.exports.marketChange=(room)=>{
    //sell
    for (let storeKey in room.terminal.store) {
        room.terminal.usedStore(storeKey)>5*1000
    }
    Game.market.getAllOrders(order => order.resourceType === RESOURCE_KEANIUM &&
        order.type === ORDER_BUY &&
        Game.market.calcTransactionCost(1000, room.roomName, order.roomName) < 500);
}